// import request from 'flyio';
import request from '@/uni_modules/u-ajax/js_sdk'
import {
    TOKEN
} from '@/constant/index.js';
import {
    reLaunch
} from '@/utils/routerSkip.js';

let rquestUrl = ''
// 请求拦截器
request.interceptors.request.use((req) => {
    console.info("请求数据", req.url, {
        address: req
    })
    let token = uni.getStorageSync(TOKEN);
    if (token) {
        // #ifdef APP-PLUS
        req.header["Cookie"] = "token=" + token;
        // #endif
        req.header["token"] = token;
    }
    // 请求参数带上语言坏境
    let langType = uni.getLocale();
    let lang = langType.includes('zh') ? 'zh_CN' : langType.includes('en') ?'en_US':'es_ES';
    console.log(lang, 'requestlang')
    let flag = req.url.indexOf('?') !== -1;
    req.url = req.url + (flag ? '&lang=' : '?lang=') + lang;
    rquestUrl = req.url
    return req
})
// 响应拦截器
request.interceptors.response.use((req) => {
    console.info("响应数据", {
        address: req
    })
    if (typeof (req.data) != 'string') {
        req.data.rquestUrl = rquestUrl || ''
    }
    let redirectPage = 0;
    var pages = getCurrentPages() // 获取栈实例
    let currentRoute = pages[pages.length - 1].route; // 获取当前页面路由
    console.log(currentRoute)
    if (currentRoute != 'pages/login/login') {
        //过期了
        if (req.data ==
            "<a href='/system/login'>.</a><script type=\"text/javascript\">parent.window.location.href='/system/login';</script>" &&
            !req.config.url.includes('system/config/getDetailByCode')) {
            if (redirectPage == 0) {
                redirectPage = 1
                uni.showToast({
                    icon: 'none',
                    title: "login expire，please login again",
                    complete: () => {
                        setTimeout(() => {
                            uni.navigateTo({
                                url: "/pages/login/login",
                                success: () => {
                                    redirectPage = 0
                                },
                                fail: () => {
                                    redirectPage = 0
                                }
                            })
                        }, 1500)

                    }
                })
            }

        }

    }

    return req.data
}, function (error) {
	console.log("error",error)
	var pages = getCurrentPages() // 获取栈实例
	let currentRoute = pages[pages.length - 1].route; // 获取当前页面路由
	 if (currentRoute != 'pages/login/login') {
		 if (error?.statusCode== 401) {
		     // 用户未经授权或者身份认证失败，需要重新登录
		     uni.showToast({
		         icon: 'none',
		         title: "login expire，please login again",
		         complete: () => {
		             setTimeout(() => {
		                 uni.navigateTo({
		                     url: "/pages/login/login",
		                     success: () => {
		                         
		                     },
		                     fail: () => {
		                        
		                     }
		                 })
		             }, 1500)
		         }
		     })
		 }
		 }
   
    return Promise.reject(error);
})

export {
    request
}
